package com.tuojiahr.service.impl;

import java.util.Date;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tuojiahr.common.HibernatePageCommon;
import com.tuojiahr.domain.TuoJiaHrSpecialColumn;
import com.tuojiahr.pagemodel.PageModel;
import com.tuojiahr.service.SpecialColumnService;
import com.tuojiahr.tools.StringUtil;

/**
 * 说明：
 *
 * @author wang.haibo
 * @date 2012-11-10 下午3:49:38
 */

@Transactional
public class SpecialColumnServiceImpl extends HibernatePageCommon implements
		SpecialColumnService {

	//添加
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void addSpecialColumn(TuoJiaHrSpecialColumn specialColumn) {
		Date now = new Date();
		specialColumn.setCreateTime(now);
		specialColumn.setUpdateTime(now);
		getHibernateTemplate().persist(specialColumn);
	}

	//更新
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void updateSpecialColumn(TuoJiaHrSpecialColumn specialColumn) {
		specialColumn.setUpdateTime(new Date());
		getHibernateTemplate().update(specialColumn);
	}

	//删除
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public String deleteSpecialColumn(long id) {
		TuoJiaHrSpecialColumn specialColumn = getOrdinaryColumnById(id);
		if(null != specialColumn) {
			if(0 == specialColumn.getChrildEntrySet().size()) {
				getHibernateTemplate().delete(specialColumn);
				return "success";
			}
		}
		return "fault";
	}

	//根据id查询
	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public TuoJiaHrSpecialColumn getOrdinaryColumnById(long id) {
		return getHibernateTemplate().load(TuoJiaHrSpecialColumn.class, id);
	}

	//根据名称模糊查询，分页显示
	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public PageModel getSpecialColumn(String keyWord) {
		if(StringUtil.isNotBlank(keyWord)) {
			return this.searchPaginated("from TuoJiaHrSpecialColumn sc where sc.name like ? order by sc.orderNum", keyWord);
		}
		return this.searchPaginated("from TuoJiaHrSpecialColumn sc order by sc.orderNum");
	}
}
